package com.feng.show.service.impl;

import com.feng.show.comm.ServerResponse;
import com.feng.show.dao.AgentMapper;
import com.feng.show.dao.ProductTypeMapper;
import com.feng.show.dao.RfidMapper;
import com.feng.show.entity.*;
import com.feng.show.service.ICountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service("ICountService")
public class CountServerImpl implements ICountService {

    @Autowired
    AgentMapper agentMapper;

    @Autowired
    RfidMapper rfidMapper;

    @Autowired
    ProductTypeMapper productTypeMapper;


    @Override
    public ServerResponse<List<Agent>> searchAllAgent(String uerId, Agent agent) {
        AgentExample example = new AgentExample();
        AgentExample.Criteria criteria = example.createCriteria();
        if (agent != null) {
            String company = agent.getCompany();
            //公司名称查询
            if (company != null && company.length() > 0) {
                criteria.andCompanyLike("%" + company + "%");
            }
            //地区
            String area = agent.getArea();
            if (area != null && area.length() > 0) {
                criteria.andAreaLike("%" + area + "%");
            }

            //根据联系人
            String contact = agent.getContact();
            if (contact != null && contact.length() > 0) {
                criteria.andContactLike("%" + contact + "%");
            }
        }
        List<Agent> agents = agentMapper.selectByExample(example);
        if (agents != null && agents.size() > 0) {
            //查询到数据
            return ServerResponse.createBySuccesMsg(agents);

        } else {
            return ServerResponse.createBySuccessMsg("没有数据");
        }

    }

    @Override
    public ServerResponse<List<RfidData>> searchAllRfid(String uerId, RfidData rfid) {

        /*
        传递参数
        1.根据时间段筛选
         */
        RfidExample rfidExample = new RfidExample();
        RfidExample.Criteria criteria = rfidExample.createCriteria();
        rfidExample.setOrderByClause("increat_time desc");

        //必须是没有被删除的
        criteria.andFlagDelEqualTo("0");



        if (rfid != null) {

            //时间段，进行时间段的筛选
            Date dataStart = rfid.getDateStart();
            Date dataEnd = rfid.getDateEnd();
            if (dataStart != null && dataEnd != null) {
                criteria.andIncreatTimeBetween(dataStart, dataEnd);
            }


            //经销商id
            Integer agentId = rfid.getAgentId();
            if (agentId != null) {
//                agent = agentMapper.selectByPrimaryKey(agentId);
                criteria.andAgentIdEqualTo(agentId);
            }

            //产品条件
            Integer prodcutId=rfid.getProductId();
            if(prodcutId!=null){
                criteria.andProductIdEqualTo(prodcutId);
            }


            //rfids啥参数都没有传递，查询所有
            List<Rfid> rfids5 = rfidMapper.selectByExample(rfidExample);
            List<RfidData> all5 = new ArrayList<>();


            //agents
            List<Agent> agents = agentMapper.selectByExample(null);
            HashMap<Integer, Agent> agentHash = new HashMap();
            for (int i = 0; i < agents.size(); i++) {
                Agent agent0 = agents.get(i);
                agentHash.put(agent0.getId(), agent0);
            }
            //products
            List<ProductType> productTypes = productTypeMapper.selectByExample(null);
            HashMap<Integer, ProductType> productHash = new HashMap<>();

            for (int i = 0; i < productTypes.size(); i++) {
                ProductType p0 = productTypes.get(i);
                productHash.put(p0.getId(), p0);
            }


            if (rfids5 != null && rfids5.size() > 0) {
                for (int i = 0; i < rfids5.size(); i++) {
                    Rfid rfidItem = rfids5.get(i);
                    RfidData rfidDataItem = new RfidData();

                    //经销商
                    Integer agentId2 = rfidItem.getAgentId();
                    Agent agent = agentHash.get(agentId2);
                    //  rfidDataItem.setAgent(agent);
                    rfidDataItem.setCompanyName(agent.getCompany());//经销商的公司


                    //添加产品名称
                    Integer productId = rfidItem.getProductId();
                    ProductType productType = productHash.get(productId);
                    rfidDataItem.setProductName(productType.getName());


                    rfidDataItem.setIncreatTime(rfidItem.getIncreatTime());
                    rfidDataItem.setTid(rfidItem.getTid());
                    rfidDataItem.setId(rfidItem.getId());

                    all5.add(rfidDataItem);


                }

                return ServerResponse.createBySuccesMsg(all5);

            } else {
                return ServerResponse.createBySuccessMsg("无数据");
            }


//            //时间段，进行时间段的筛选
//            Date dataStart = rfid.getDateStart();
//            Date dataEnd = rfid.getDateEnd();
//
//            if (dataStart != null && dataEnd != null) {
//                criteria.andIncreatTimeBetween(dataStart, dataEnd);
//            }
//
//
//            //判断agentId
//            Integer agentId;
//            Agent agentGet = rfid.getAgent();
//            if (agentGet != null) {
//                agentId = agentGet.getId();
//            } else {
//                agentId = rfid.getAgentId();
//            }
//
//
//            Agent agent = null;
//            if (agentId != null) {
//                agent = agentMapper.selectByPrimaryKey(agentId);//
////            criteria.andAgentIdEqualTo(agentId);
//
//
//            }
//            List<Rfid> rfids = rfidMapper.selectByExample(rfidExample);
//
//        /*
//        1.如果传递过来了经销商，（那就是查询指定经销商的信息，就这边就不需要返回经销商信息）
//        2.如果没有传递经销商， （那就需要遍历了）
//         */
//
//            List<RfidData> rfidDataList = new ArrayList<>();
//            RfidData rfidDataItem;
//
//
//            //传递agentId，且查询到数据的
//            if (agent != null) {
//                if (rfids != null && rfids.size() > 0) {
//                    for (int i = 0; i < rfids.size(); i++) {
//                        Rfid rfidItem = rfids.get(i);
//
//                        rfidDataItem = new RfidData();
//                        rfidDataItem.setTid(rfidItem.getTid());
//                        rfidDataItem.setTid(rfidItem.getTid());
//                        rfidDataItem.setIncreatTime(rfidItem.getIncreatTime());
//                        rfidDataItem.setCompanyName(agent.getCompany());
//
//                        //   rfidDataItem.setAgent(agent);
//                        rfidDataList.add(rfidDataItem);
//                    }
//                    return ServerResponse.createBySuccesMsg(rfidDataList);
//                } else {
//                    return ServerResponse.createBySuccessMsg("没有符合条件的数据");
//                }
//
//            } else {
//                //没有传递agentId,那就进行全部查询
//                if (rfids != null && rfids.size() > 0) {
//                    for (int i = 0; i < rfids.size(); i++) {
//                        Rfid rfidItem = rfids.get(i);
//
//                        rfidDataItem = new RfidData();
//                        rfidDataItem.setTid(rfidItem.getTid());
//                        rfidDataItem.setIncreatTime(rfidItem.getIncreatTime());
//
//                        Integer agentIdChecked = rfidItem.getAgentId();
//                        if (agentIdChecked != null) {
//                            Agent agentChcked = agentMapper.selectByPrimaryKey(agentIdChecked);
//                            if (agentChcked != null) {
//                                rfidDataItem.setAgent(agentChcked);
//                            }
//
//                        }
//                        rfidDataList.add(rfidDataItem);
//                    }
//                    return ServerResponse.createBySuccesMsg(rfidDataList);
//                } else {
//                    return ServerResponse.createBySuccessMsg("没有符合条件的数据");
//                }
//
//            }
        } else {
            //rfids啥参数都没有传递，查询所有
            List<Rfid> rfids = rfidMapper.selectByExample(null);
            List<RfidData> all = new ArrayList<>();


            //agents
            List<Agent> agents = agentMapper.selectByExample(null);
            HashMap<Integer, Agent> agentHash = new HashMap();
            for (int i = 0; i < agents.size(); i++) {
                Agent agent0 = agents.get(i);
                agentHash.put(agent0.getId(), agent0);
            }
            //products
            List<ProductType> productTypes = productTypeMapper.selectByExample(null);
            HashMap<Integer, ProductType> productHash = new HashMap<>();

            for (int i = 0; i < productTypes.size(); i++) {
                ProductType p0 = productTypes.get(i);
                productHash.put(p0.getId(), p0);
            }


            if (rfids != null && rfids.size() > 0) {
                for (int i = 0; i < rfids.size(); i++) {
                    Rfid rfidItem = rfids.get(i);
                    RfidData rfidDataItem = new RfidData();

                    //经销商
                    Integer agentId = rfidItem.getAgentId();
                    Agent agent = agentHash.get(agentId);
                    //  rfidDataItem.setAgent(agent);
                    rfidDataItem.setCompanyName(agent.getCompany());//经销商的公司


                    //添加产品名称
                    Integer productId = rfidItem.getProductId();
                    ProductType productType = productHash.get(productId);
                    if(productType!=null){
                        rfidDataItem.setProductName(productType.getName());
                    }
                    rfidDataItem.setIncreatTime(rfidItem.getIncreatTime());
                    rfidDataItem.setTid(rfidItem.getTid());
                    rfidDataItem.setId(rfidItem.getId());

                    all.add(rfidDataItem);


                }

                return ServerResponse.createBySuccesMsg(all);

            } else {
                return ServerResponse.createBySuccessMsg("暂无数据");
            }


        }
    }

}
